import React, { useEffect, useRef } from 'react'
import plugin from '../plugin'
import { ApplyPluginsType, useModel } from '@sui/sui';

export default function Provider(props){
    const { children, loading:loadingElement } = props;
    const appLoaded = useRef(false);
    const { loading = false } = useModel("context") || {};
    // 获取runtime配置
    const useRuntimeConfig = plugin.applyPlugins({
        key: "getRuntimeConfig",
        type: ApplyPluginsType.filter,
        initialValue: {},
        args: {
          loading
        }
      }) || {};

    useEffect(() => {
      if (!loading) {
        appLoaded.current = true;
      }
    }, [loading]);
    if (loading && !appLoaded.current) {
      return loadingElement || useRuntimeConfig.loading || null;
    }
    return children
};

Provider.displayName = 'ContextProvider';